<?php

namespace App\Http\Controllers\Admins\Order;

use App\Http\Controllers\Admins\BaseAdminController;
use App\Models\Admins;
use App\Models\Organization;
use Carbon\Carbon;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
use Illuminate\Http\Request;

class EnterpriseController extends BaseAdminController
{
    /**
     * @param Request $request
     * @return array|Application|Factory|View
     */
    public function index(Request $request)
    {
        $condition = $request->get('condition','short_name');
        if($request->isMethod('post')){
            # 要执行的代码
//            $roles = array('center','is_super_user');
            $query = Organization::query()->latest();
            $keywords = $request->get('keywords',null);
            if ($keywords) {
                $query->where($condition, 'like', '%' . $keywords . '%');
            }
            $status = $request->get('status','all');
            if($status != 'all'){
                $query->where('status', $status);
            }
            $pageSize = $request->get('limit',10);
            # 计算offset值
            $offset = $request->get('offset',0);
            # 总数
            $total = $query->count();
            $data = $query->limit($pageSize)->offset($offset)->get();
            $result = array();
            if($data){
                foreach($data as $val){
                    $status_show = '否';
                    if($val['status'] == 1){
                        $status_show = '是';
                    }
                    $result[] = [
                        'id'=>$val['id'],
                        'name'=>$val['name'],
                        'short_name'=>$val['short_name'],
                        'operation_head'=>$val['operation_head'],
                        'lead'=>$val['lead'],
                        'address'=>$val['address'],
                        'status_show'=>$status_show,
                        'brief'=>$val['brief'],
                        'admin_name'=>Admins::query()->where('id',$val['add_admin_id'])->value('username')?:'-',
                        'crated_at'=>Carbon::parse($val['crated_at'])->toDateTimeString(),
                    ];
                }
            }
            return ret([
                'total'=>$total,
                'data'=>$result
            ]);
        }
        $admin_id = $this->admin_id;
        $subtitle = '企业管理';
        $compact = compact('subtitle','admin_id');
        return view('v1.admin.order.enterprise.index',$compact);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
